package net.atomarrow.db.parser;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.atomarrow.bean.Pager;
import net.atomarrow.db.parser.batch.BatchSqlHolder;
import net.atomarrow.domains.Domain;
import net.atomarrow.util.StringLink;
/**
 * 解析器
 * @author Michael
 *
 */
public class BaseParser implements Parser {
    /**
     * 语法解析
     *
     * @param sb
     * @return
     */
    protected String parse(Conditions conditions, StringBuilder sb) {
    	return parse(conditions, sb, false);
    }

    /**
     * 语法解析
     *
     * @param sb
     * @return
     */
    protected String parse(Conditions conditions, StringBuilder sb,boolean noOrder) {
        if (conditions.haveOrder && conditions.haveGroup) {
            this.processGroupOrder(conditions);
            return groupParse(conditions, sb);
        } else if (conditions.haveOrder) {
            this.processOrder(conditions);
        }
        for (Condition conditon : conditions.cs) {
            if (conditions.link == null) {
                conditions.link = "";
            }
            if (conditon.getLink() != null && (!conditon.getLink().equals(""))) {
                if (conditon.getLink().equals("(")) {
                    conditions.parenthesesStart = true;
                } else if (conditon.getLink().equals("((")) {
                	conditions.parenthesesDoubleStart = true;
                } else if (conditon.getLink().equals(")")) {
                    sb.append(")");
                } else {
                    conditions.link = conditon.getLink();
                }
                continue;
            }
            if (conditon.getCompareState().equals(Conditions.EW)
                    || conditon.getCompareState().equals(Conditions.GT)
                    || conditon.getCompareState().equals(Conditions.ST)) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());

            } else if (conditon.getCompareState().equals(Conditions.BW)) {// between
                                                                          // 的处理方法
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());
                sb.append(" and ");
                sb.append(conditon.getValue2());

            } else if (conditon.getCompareState().equals(Conditions.OR)) {// or
                                                                          // 的处理方法
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append("(");
                sb.append(conditon.getFieldName());
                sb.append("=");
                sb.append(conditon.getValue1());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getFieldName());
                sb.append("=");
                sb.append(conditon.getValue2());
                sb.append(")");

            } else if (conditon.getCompareState().equals(Conditions.IN)) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" in(");
                int i = 0;
                if (conditon.getValue1().getClass().isArray()) {
                    // 是数组情况
                    Object[] arrays = (Object[]) conditon.getValue1();
                    for (Object a : arrays) {
                        if (i == arrays.length - 1) {
                            sb.append(processValue((Serializable) a));
                        } else {
                            sb.append(processValue((Serializable) a));
                            sb.append(",");
                        }
                        i++;
                    }
                } else {
                    // 是语句情况
                    sb.append(getColumnHql((Conditions) conditon.getValue2(),
                            (String) conditon.getValue1()));
                }

                sb.append(")");

            } else if (conditon.getCompareState().equals(Conditions.NOTIN)) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" not in(");
                int i = 0;
                if (conditon.getValue1().getClass().isArray()) {
                    // 是数组情况
                    Object[] arrays = (Object[]) conditon.getValue1();
                    for (Object a : arrays) {
                        if (i == arrays.length - 1) {
                            sb.append(processValue((Serializable) a));
                        } else {
                            sb.append(processValue((Serializable) a));
                            sb.append(",");
                        }
                        i++;
                    }
                } else {
                    // 是语句情况
                    sb.append(getColumnHql((Conditions) conditon.getValue2(),
                            (String) conditon.getValue1()));
                }

                sb.append(")");

            } else if (conditon.getCompareState().equals(Conditions.LIKE)) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" like ");
                sb.append(conditon.getValue1());

            } else if (conditon.getCompareState().equals(Conditions.NOTLIKE)) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" not like ");
                sb.append(conditon.getValue1());

            } else if (conditon.getCompareState().equals(Conditions.EXP)) {
                sb.append(conditon.getValue1());
            } else if (conditon.getCompareState().equals(Conditions.GROUP)) {
                if (conditions.cs.size() == 1) {
                    if(sb.length()>0){
                        sb.delete(sb.length()-" where ".length(), sb.length());
                    }
                    return sb.append(" group by ").append(conditon.getFieldName()).toString();
                }
                // sb = new StringBuilder(sb.substring(0, sb.length() - 5));
                sb.append(" group by ");
                sb.append(conditon.getFieldName());
            } else if (conditon.getCompareState().equals(Conditions.ASC)) {
            	if(noOrder){
            		continue;
            	}
                if (conditions.cs.size() == 1) {
                    if(sb.length()>0){
                        sb.delete(sb.length()-" where ".length(), sb.length());
                    }
                    return sb.append(" order by ").append(conditon.getFieldName()).append(" ").append(Conditions.ASC).toString();
                }
                // sb = new StringBuilder(sb.substring(0, sb.length() - 5));
                sb.append(" order by ");
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(Conditions.ASC);
                return sb.toString();
            } else if (conditon.getCompareState().equals(Conditions.DESC)) {
            	if(noOrder){
            		continue;
            	}
                if (conditions.cs.size() == 1) {
                    if(sb.length()>0){
                        sb.delete(sb.length()-" where ".length(), sb.length());
                    }
                    return sb.append(" order by ").append(conditon.getFieldName()).append(" ").append(Conditions.DESC).toString();
                }
                // sb = new StringBuilder(sb.substring(0, sb.length() - 5));
                sb.append(" order by ");
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(Conditions.DESC);

                return sb.toString();

            } else if (conditon.getCompareState().equals(Conditions.FIND_IN_SET)  ) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(" find_in_set(");
                sb.append(conditon.getFieldName()).append(",").append(conditon.getValue1());
                sb.append(") > 0");

            } else if (conditon.getCompareState().equals(Conditions.FIND_NOT_IN_SET)  ) {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(" (find_in_set(");
                sb.append(conditon.getFieldName()).append(",").append(conditon.getValue1());
                sb.append(") = 0 or find_in_set(");
                sb.append(conditon.getFieldName()).append(",").append(conditon.getValue1());
                sb.append(") is null)");

            } else {
                processSqlLink(conditions, sb);// 处理 and or （） 等连接
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());

            }

            conditions.link = Conditions.AND;

        }
        String str = sb.toString();
        return str;
    }

    private void processSqlLink(Conditions conditions, StringBuilder sb) {
        sb.append(" ");
        sb.append(conditions.link);
        sb.append(" ");
        if (conditions.parenthesesStart) {
            sb.append("(");
            conditions.parenthesesStart = false;
        }else if (conditions.parenthesesDoubleStart) {
            sb.append("((");
            conditions.parenthesesDoubleStart = false;
        }
    }

    private String groupParse(Conditions conditions, StringBuilder sb) {
        for (Condition conditon : conditions.cs) {
            if (conditions.link == null) {
                conditions.link = "";
            }
            if (conditon.getLink() != null && (!conditon.getLink().equals(""))) {
                if (conditon.getLink().equals("(")) {
                    conditions.parenthesesStart = true;
                } else if (conditon.getLink().equals("((")) {
                	conditions.parenthesesDoubleStart = true;
                } else if (conditon.getLink().equals(")")) {
                    sb.append(")");
                } else {
                    conditions.link = conditon.getLink();
                }
                continue;
            }
            if (conditon.getCompareState().equals(Conditions.EW)
                    || conditon.getCompareState().equals(Conditions.GT)
                    || conditon.getCompareState().equals(Conditions.ST)) {
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());

            } else if (conditon.getCompareState().equals(Conditions.BW)) {// between
                                                                          // 的处理方法
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());
                sb.append(" and ");
                sb.append(conditon.getValue2());

            } else if (conditon.getCompareState().equals(Conditions.OR)) {// or
                                                                          // 的处理方法
                processSqlLink(conditions, sb);
                sb.append("(");
                sb.append(conditon.getFieldName());
                sb.append("=");
                sb.append(conditon.getValue1());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getFieldName());
                sb.append("=");
                sb.append(conditon.getValue2());
                sb.append(")");

            } else if (conditon.getCompareState().equals(Conditions.LIKE)) {
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" like ");
                sb.append(conditon.getValue1());
            } else if (conditon.getCompareState().equals(Conditions.NOTLIKE)) {
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" not like ");
                sb.append(conditon.getValue1());
            } else if (conditon.getCompareState().equals(Conditions.IN)) {
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" in(");
                int i = 0;
                if (conditon.getValue1().getClass().isArray()) {
                    // 是数组情况
                    Object[] arrays = (Object[]) conditon.getValue1();
                    for (Object a : arrays) {
                        if (i == arrays.length - 1) {
                            sb.append(processValue((Serializable) a));
                        } else {
                            sb.append(processValue((Serializable) a));
                            sb.append(",");
                        }
                        i++;
                    }
                } else {
                    // 是语句情况
                    sb.append(getColumnHql((Conditions) conditon.getValue2(),
                            (String) conditon.getValue1()));
                }
                sb.append(")");

            } else if (conditon.getCompareState().equals(Conditions.GROUP)) {
                if (conditions.cs.size() == 2) {
                    sb = new StringBuilder(sb.substring(0, sb.length() - 6));
                    sb.append(" group by ");
                    sb.append(conditon.getFieldName());
                } else {
                    // sb = new StringBuilder(sb.substring(0, sb.length() - 5));
                    sb.append(" group by ");
                    sb.append(conditon.getFieldName());
                }

            } else if (conditon.getCompareState().equals(Conditions.ASC)) {
                if (conditions.cs.size() == 1) {
                    return new StringLink("from ", conditions.getTableName(),conditions.getJoin(),
                            " order by ", conditon.getFieldName(), " ",
                            Conditions.ASC).toString();
                }
                sb.append(" order by ");
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(Conditions.ASC);
                return sb.toString();
            } else if (conditon.getCompareState().equals(Conditions.DESC)) {
                if (conditions.cs.size() == 1) {
                    return new StringLink("from ", conditions.getTableName(),conditions.getJoin(),
                            " order by ", conditon.getFieldName(), " ",
                            Conditions.DESC).toString();
                }
                sb.append(" order by ");
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(Conditions.DESC);

                return sb.toString();

            } else {
                processSqlLink(conditions, sb);
                sb.append(conditon.getFieldName());
                sb.append(" ");
                sb.append(conditon.getCompareState());
                sb.append(" ");
                sb.append(conditon.getValue1());

            }
            conditions.link = Conditions.AND;
        }

        String str = sb.toString();
        str = str.substring(0, str.length() - 5);
        return str;
    }

    /**
     * 处理排序放在最后group倒数第二
     *
     * @param condition
     * @return
     */
    private void processGroupOrder(Conditions conditions) {
        Condition condition = conditions.cs.get(conditions.cs.size() - 1);
        // 最后一个条件是排序的情况
        if (condition.getCompareState().equals(Conditions.DESC)
                || condition.getCompareState().equals(Conditions.ASC)) {
            condition = conditions.cs.get(conditions.cs.size() - 2);
            if (condition.getCompareState().equals(Conditions.GROUP)) {
                return;
            }

        } else {// 最后一个条件不是排序
            Condition o = null;
            Condition g = null;
            for (int i = 0; i < conditions.cs.size(); i++) {
                if (conditions.cs.get(i).getCompareState()
                        .equals(Conditions.DESC)
                        || conditions.cs.get(i).getCompareState()
                                .equals(Conditions.ASC)) {
                    o = conditions.cs.remove(i);
                    break;
                }
            }
            for (int i = 0; i < conditions.cs.size(); i++) {
                if (conditions.cs.get(i).getCompareState()
                        .equals(Conditions.GROUP)) {
                    g = conditions.cs.remove(i);
                    break;
                }
            }
            conditions.cs.add(g);
            conditions.cs.add(o);
        }
    }

    /**
     * 处理排序放在最后
     *
     * @param condition
     * @return
     */
    private void processOrder(Conditions conditions) {
        Condition condition = conditions.cs.get(conditions.cs.size() - 1);
        // 最后一个条件是排序的情况
        if (condition.getCompareState().equals(Conditions.DESC)
                || condition.getCompareState().equals(Conditions.ASC)) {
            return;
        } else {// 最后一个条件不是排序
            Condition c = null;
            for (int i = 0; i < conditions.cs.size(); i++) {
                if (conditions.cs.get(i).getCompareState()
                        .equals(Conditions.DESC)
                        || conditions.cs.get(i).getCompareState()
                                .equals(Conditions.ASC)) {
                    c = conditions.cs.remove(i);
                    break;
                }
            }
            conditions.cs.add(c);
        }
    }

    /**
     * 得到搜索的HQl语句
     */
    public String getSelectHql(Conditions conditions) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return "from " + conditions.getTableName()+conditions.getJoin();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        return parse(conditions, sb);
    }

    /**
     * 得到删除的HQl语句
     *
     * @return 语句
     */
    public String getDelHql(Conditions conditions) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return "delete from " + conditions.getTableName();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("delete from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 得到搜索数量的HQl语句
     *
     * @return 语句
     */
    public String getDistinctCountHql(Conditions conditions,String attribute) {

    	if (conditions.sql != null) {
    		return conditions.sql;
    	}

    	conditions.link = null;
    	if (conditions.cs.size() == 0) {
    		return conditions.getSelect().concat("count(distinct "+attribute+") from ")
    				.concat(conditions.getTableName()).concat(conditions.getJoin());
    	}
    	String str;
    	if(conditions.haveGroup){
    		//select count(1) as total from (select count(*) from tbaccount group by deptId) as temp
    		StringBuilder sb = new StringBuilder();
    		sb.append("select count(1) as total from (");
    		sb.append(conditions.getSelect());
    		sb.append("count(distinct ");
    		sb.append(attribute);
    		sb.append(" ) from ");
    		sb.append(conditions.getTableName());
    		sb.append(" ");
    		sb.append(conditions.getJoin());
    		sb.append(" where ");
    		str=parse(conditions, sb);
    		str+=") as temp";
    	}else{
    		StringBuilder sb = new StringBuilder();
    		sb.append(conditions.getSelect());
    		sb.append("count(distinct ");
    		sb.append(attribute);
    		sb.append(" ) from ");
    		sb.append(conditions.getTableName());
    		sb.append(" ");
    		sb.append(conditions.getJoin());
    		sb.append(" where ");
    		str=parse(conditions, sb);
    	}
    	return str;
    }
    /**
     * 得到搜索数量的HQl语句
     *
     * @return 语句
     */
    public String getCountHql(Conditions conditions) {

        if (conditions.sql != null) {
            return conditions.sql;
        }

        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return conditions.getSelect().concat("count(*) from ")
                    .concat(conditions.getTableName()).concat(conditions.getJoin());
        }
        String str;
        if(conditions.haveGroup){
            //select count(1) as total from (select count(*) from tbaccount group by deptId) as temp
            StringBuilder sb = new StringBuilder();
            sb.append("select count(1) as total from (");
            sb.append(conditions.getSelect());
            sb.append("count(*) from ");
            sb.append(conditions.getTableName());
            sb.append(" ");
            sb.append(conditions.getJoin());
            sb.append(" where ");
            str=parse(conditions, sb,true);
            if(str.endsWith(" where ")){
            	str+="1=1";
            }
            str+=") as temp";
        }else{
            StringBuilder sb = new StringBuilder();
            sb.append(conditions.getSelect());
            sb.append("count(*) from ");
            sb.append(conditions.getTableName());
            sb.append(" ");
            sb.append(conditions.getJoin());
            sb.append(" where ");
            str=parse(conditions, sb,true);
            if(str.endsWith(" where ")){
            	str+="1=1";
            }
        }
        return str;
    }

    /**
     * 得到搜索某列最大值的HQl语句
     *
     * @return 语句
     */
    public String getMaxHql(Conditions conditions, String attribute) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return new StringLink(conditions.getSelect(), "max(", attribute,
                    ") from ", conditions.getTableName(),conditions.getJoin()).toString();
        }
        StringBuilder sb = new StringBuilder();
        sb.append(conditions.getSelect());
        sb.append("max(");
        sb.append(attribute);
        sb.append(") from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 得到搜索某列最大值行信息的HQl语句
     *
     * @return 语句
     */
    public String getMaxDomainHql(Conditions conditions, String attribute) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return new StringLink("from ", conditions.getTableName(),conditions.getJoin(),
                    " order by ", attribute, " ", Conditions.DESC).toString();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");
        conditions.put(attribute, Conditions.DESC);
        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 得到搜索某列数量和的HQl语句
     *
     * @return 语句
     */
    public String getSumHql(Conditions conditions, String attribute) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return new StringLink(conditions.getSelect(), "sum(", attribute,
                    ") from ", conditions.getTableName(),conditions.getJoin()).toString();
        }
        StringBuilder sb = new StringBuilder();
        sb.append(conditions.getSelect());
        sb.append("sum(");
        sb.append(attribute);
        sb.append(") from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 得到搜索不重复列HQL
     *
     * @param attribute
     * @return 语句
     */
    public String getDistinctHql(Conditions conditions, String attribute) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return new StringLink(conditions.getSelect(), "distinct ",
                    attribute, " from ", conditions.getTableName(),conditions.getJoin()).toString();
        }
        StringBuilder sb = new StringBuilder();
        sb.append(conditions.getSelect());
        sb.append("distinct ");
        sb.append(attribute);
        sb.append(" from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    public String getColumnHql(Conditions conditions, String attribute) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (conditions.cs.size() == 0) {
            return new StringLink(conditions.getSelect(), attribute, " from ",
                    conditions.getTableName(),conditions.getJoin()).toString();
        }
        StringBuilder sb = new StringBuilder();
        sb.append(conditions.getSelect());
        sb.append(attribute);
        sb.append(" from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 查询返回包含多个列的实体语句
     *
     * @param columns
     * @return hql语句
     */
    public String getMulColumnHql(Conditions conditions, String[] columns) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        if (columns == null || columns.length == 0) {
            return this.getSelectHql(conditions);
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < columns.length; i++) {
            sb.append(columns[i]);
            if (i != columns.length - 1) {
                sb.append(",");
            }
        }
        String cols = sb.toString();
        if (conditions.cs.size() == 0) {
            return new StringLink(conditions.getSelect(), "new ",
                    conditions.getTableName(), "(", sb.toString(), ") from ",
                    conditions.getTableName(),conditions.getJoin()).toString();
        }

        sb.setLength(0);
        sb.append(conditions.getSelect());
        sb.append("new ");
        sb.append(conditions.getTableName());
        sb.append("(");
        sb.append(cols);
        sb.append(") from ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 执行更新多列的语句
     *
     * @param columns
     * @return hql语句
     */
    public String getModifyColumnHql(Conditions conditions,
            Serializable[] keyValues) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyValues.length; i++) {
            if (i % 2 == 0) {
                sb.append(keyValues[i]);
                sb.append("=");
            } else {
                Serializable ser = processValue(keyValues[i]);
                sb.append(ser);
                sb.append(",");
            }
        }
        if (sb.lastIndexOf(",") == sb.length() - 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        String cols = sb.toString();
        if (conditions.cs.size() == 0) {
            return new StringLink("update ", conditions.getTableName(),conditions.getJoin(),
                    " set ", cols, "").toString();
        }
        sb.setLength(0);
        sb.append("update ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" set ");
        sb.append(cols);
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 执行更新多列的语句
     *
     * @param columns
     * @return hql语句
     */
    public String getModifyColumnHql(Conditions conditions,
            Map<String, Serializable> columns) {
        if (conditions.sql != null) {
            return conditions.sql;
        }
        conditions.link = null;
        StringBuilder sb = new StringBuilder();
        Iterator<String> iter = columns.keySet().iterator();
        while (iter.hasNext()) {
            String s = iter.next();
            sb.append(s);
            sb.append("=");

            Serializable ser = processValue(columns.get(s));
            sb.append(ser);

            if (iter.hasNext()) {
                sb.append(",");
            }
        }
        String cols = sb.toString();
        if (conditions.cs.size() == 0) {
            return new StringLink("update ", conditions.getTableName(),conditions.getJoin(),
                    " set ", cols, "").toString();
        }
        sb.setLength(0);
        sb.append("update ");
        sb.append(conditions.getTableName());
        sb.append(" ");
        sb.append(conditions.getJoin());
        sb.append(" set ");
        sb.append(cols);
        sb.append(" where ");

        String str = parse(conditions, sb);
        return str;
    }

    /**
     * 处理字符串格式
     *
     * @param value
     * @return
     */
    protected Serializable processValue(Serializable value) {
        return ValueProcessor.processValue(value);
    }

    @Override
    public String getAddSql(Domain domain) {
        return getAddSql(null, domain);
    }

    @Override
    public String getAddSql(String tableName, Domain domain) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public String getUpdateSql(String tableName, Domain domain) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public String getSelectPagerSql(Conditions conditions, Pager pager) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public BatchSqlHolder getBatchAddSql(String tableName,List<? extends Domain> dataList) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public String getMulColumnPagerSql(Conditions conditions, String[] columns,
            Pager pager) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public String getColumnPagerSql(Conditions conditions, String attribute,
            Pager pager) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public BatchSqlHolder getBatchUpdateSql(String tableName,List<? extends Domain> dataList) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public BatchSqlHolder getBatchUpdateSql(String tableName,String[] cols,List<? extends Domain> datas) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }

    @Override
    public String getGroupConcatSql(Conditions conditions, String attribute,
            String separator) {
        throw new RuntimeException("不能调用此方法，需要调用相应的实现类");
    }
}
